package com.zafu.dao;

import com.zafu.entity.CartGoodsInfo;
import com.zafu.entity.StoreInfo;
import com.zafu.util.ElmDbConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * ClassName: CartGoodsBean
 * Package: com.zafu.dao
 * Description:
 *
 * @Author Wang
 * @Create 2024/12/21 11:35
 * @Version 1.0
 */
public class CartGoodsBean {
    private Connection conn;

    public CartGoodsBean() {
    }

    /**
     * 向购物车增加一条记录
     * @param info
     * @return
     */

    public int Add(CartGoodsInfo info) {
        int count = -1;
        conn =  ElmDbConn.GetConnection();

        if (conn == null) {
            System.out.println("数据库连接失败");
            return -1;
        }

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append("insert into CartGoods")
                    .append("( image, store, price, name, info, count, id)")
                    .append("values ( ?, ?, ?, ?, ?, ?, ?) ");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString() );
            int i = 0;
            st.setString(++i, info.getImage());
            st.setString(++i, info.getStore());
            st.setDouble(++i, info.getPrice());
            st.setString(++i, info.getName());
            st.setString(++i, info.getInfo());
            st.setInt(++i, info.getCount());
            st.setInt(++i, info.getId());
            count = st.executeUpdate();
            System.out.printf("插入%d条记录\n", count);
            return count;
        }
        catch (SQLException e) {
            System.out.println("插入失败："+ e.getMessage());
            e.printStackTrace();
            return -1;
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch (SQLException e) {
                    System.out.println("关闭连接失败\n" + e.getMessage());
                }
            }
        }
    }

    /**
     * 删除一条数据
     * @param id
     * @return
     */
    public int DelById(int id) {
        int count = -1; // 受影响的记录条数
        conn = ElmDbConn.GetConnection();// 获得连接

        try {
            StringBuffer sBuffer = new StringBuffer();
            // sql语句
            sBuffer.append( "delete from CartGoods " )
                    .append( " where id = ? " );
            PreparedStatement st = conn.prepareStatement(  sBuffer.toString() );
            int i = 1;
            // id, 主键
            st.setInt(i, id);
            ++i;
            // 执行sql
            count = st.executeUpdate( );// 执行语句
            System.out.printf( "删除%d条记录\n", count );
            return count;
        }
        catch(SQLException e ){
            System.out.printf( "删除失败:" + e.getMessage() );
            e.printStackTrace();
            return -1;
        }
        finally {
            if( conn != null ){
                try{
                    conn.close();
                }
                catch( SQLException e ){
                    System.out.printf( "关闭连接失败\n" + e.getMessage()  );
                }// try
            }// if
        }
    }


    /**
     * 清空数据库
     * @return
     */
    public int DelAll() {
        int count = -1; // 受影响的记录条数
        try (Connection conn = ElmDbConn.GetConnection();
             PreparedStatement st = conn.prepareStatement("DELETE FROM CartGoods")) {

            count = st.executeUpdate(); // 执行 DELETE
            System.out.printf("删除 %d 条记录\n", count);

        } catch (SQLException e) {
            System.out.printf("清空表失败: %s\n", e.getMessage());
            e.printStackTrace();
            count = -1;
        }
        return count;
    }


    /**
     * 获得所有数据
     * @return
     */
    public ArrayList<CartGoodsInfo> GetAll() {
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        ArrayList<CartGoodsInfo> list = new ArrayList<>();
        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " select image, store, price, name, info, count, id")
                    .append(" from CartGoods");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            ResultSet rs = st.executeQuery();
            while(rs.next()) {
                CartGoodsInfo info = GetDataFromResultSet(rs);
                list.add(info);
            }
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return list;
    }
    private CartGoodsInfo GetDataFromResultSet(ResultSet rs ){

        CartGoodsInfo cartGoodsInfo = null;
        try{
            String image = rs.getString( "image" );
            String store = rs.getString( "store" );
            Double price = rs.getDouble("price");
            String name = rs.getString( "name" );
            String info = rs.getString( "info" );
            Integer count = rs.getInt( "count" );
            Integer id = rs.getInt( "id" );
            cartGoodsInfo = new CartGoodsInfo(image,store,price,name,info,count,id);

        }catch( SQLException e ){
            System.out.printf( "数据库查询失败\n" + e.getMessage()  );
            e.printStackTrace();
        }
        return cartGoodsInfo;
    }
}
